package com.chuantou.yuba.ui;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;

import com.chuantou.yuba.R;
import com.chuantou.yuba.adapter.ViewPagerAdapter;
import com.chuantou.yuba.ui.fragment.ContributionFishingPlaceFragment;
import com.chuantou.yuba.ui.fragment.ContributionFishingShopFragment;
import com.chuantou.yuba.ui.widget.UploadNearPopup;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.UiUtil;
import com.viewpagerindicator.TabPageIndicator;

/**
 * 贡献界面
 * 
 * @author Administrator
 * 
 */
public class ContributionActivity extends BaseActionBarActivity {
	protected ViewPager mViewPager;
	protected TabPageIndicator mIndicator;
	private ViewPagerAdapter mViewPagerAdapter;
	private UploadNearPopup mPopup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simple_viewpager);
		setupViews();
		setTitle(R.string.contribution);
	}

	private void setupViews() {
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mViewPager.setOffscreenPageLimit(1);
		mViewPagerAdapter = getAdapter();
		mViewPager.setAdapter(mViewPagerAdapter);
		mIndicator = (TabPageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mViewPager);
	}

	private ViewPagerAdapter getAdapter() {
		ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), this);
		adapter.addPager(getString(R.string.fishing_place), ContributionFishingPlaceFragment.class.getName(), new Bundle());
		adapter.addPager(getString(R.string.fishing_shop), ContributionFishingShopFragment.class.getName(), new Bundle());
		return adapter;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_upload, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_upload:
			AccountManager accountManager = AccountManager.getInstance(getApplicationContext());
			if (!accountManager.isLogined()) {
				UiUtil.showNoLoginDialog(this, -1, LoginActivity.ACTION_RETURN);
				return true;
			}
			if (mPopup != null && mPopup.isShowing())
				return true;
			mPopup = new UploadNearPopup(this);
			ViewGroup decor = (ViewGroup) getWindow().getDecorView();
			mPopup.showAtLocation(decor, Gravity.BOTTOM, 0, 0);
			return true;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
